import { app, BrowserWindow } from 'electron';
import { electronApp, optimizer } from '@electron-toolkit/utils';
import './trayAndMenu';
import { createMainWindow, showMainWindow, mainWindow } from './windowControl/mainWindow';
import { logger } from './log/logger';
import ipcInit from './ipc';

const gotTheLock = app.requestSingleInstanceLock();
if(!gotTheLock) {
    app.quit();
}else{
    app.on('second-instance', () => {
        // 当运行第二个实例时,将会聚焦到myWindow这个窗口
        if (mainWindow) {
            showMainWindow();
        }
    });
    app.whenReady().then(() => {
    // Set app user model id for windows
        electronApp.setAppUserModelId('com.electron');

        // Default open or close DevTools by F12 in development
        // and ignore CommandOrControl + R in production.
        // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
        app.on('browser-window-created', (_, window) => {
            optimizer.watchWindowShortcuts(window);
        });

        logger.info('应用启动');
        createMainWindow();

        ipcInit();

        app.on('activate', function () {
            // On macOS it's common to re-create a window in the app when the
            // dock icon is clicked and there are no other windows open.
            if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
        });
    });
}